package com.fc.redis.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;

/**
 * @program: fc-redis->ExcelBean
 * @description: excel
 * @author: G_Y
 * @create: 2019-09-07 00:00
 **/
public abstract class ExportExcelBean<T> {

    // excel文件名称
    protected String fileName;
    // excel sheet 名称
    protected String sheetName;
    // 表头
    protected String[] tableHeader;
    // 总列数
    protected int columnCount;
    // 数据行
    protected List<T> rows;
    // 对应对象中的字段名称顺序集合
    protected List<String> fieldNames;

    public abstract void writeExportExcelRow(HSSFRow row, T t);

    public void writeExportExcelRowByFieldNames(HSSFRow row, T t) {
        // 利用那个反射实现 将每一列对应的 对象 属性值 反射获取 写入 excel
        int i = 0;
        // 需要注意顺序
        for (String fieldName : this.fieldNames) {
            try {
                Class<?> cls = t.getClass();
                Field declaredField = cls.getDeclaredField(fieldName);
                declaredField.setAccessible(true);
                row.createCell(i).setCellValue(ifNull(declaredField.get(t)));
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
                throw new RuntimeException("excel写出异常");
            }
            i++;
        }
    }

    protected String ifNull(Object obj) {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    protected ExportExcelBean(@NotBlank String fileName, @NotBlank String sheetName, @NotNull String[] tableHeader, @NotEmpty List<T> rows) {
        this.fileName = fileName;
        this.sheetName = sheetName;
        this.tableHeader = tableHeader;
        this.rows = rows;
        this.columnCount = tableHeader.length;
    }

    public List<String> getFieldNames() {
        return fieldNames;
    }

    public ExportExcelBean<T> setFieldNames(List<String> fieldNames) {
        this.fieldNames = fieldNames;
        return this;
    }

    public ExportExcelBean<T> addField(String fileName) {
        if (CollectionUtils.isEmpty(fieldNames)) {
            fieldNames = new ArrayList<>(20);
        }
        fieldNames.add(fileName);
        return this;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getSheetName() {
        return sheetName;
    }

    public void setSheetName(String sheetName) {
        this.sheetName = sheetName;
    }

    public String[] getTableHeader() {
        return tableHeader;
    }

    public void setTableHeader(String[] tableHeader) {
        this.tableHeader = tableHeader;
        this.columnCount = tableHeader.length;
    }

    public int getColumnCount() {
        return columnCount;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }
}
